﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WenSkin
{
    public partial class MsgBox : WenSkin.Forms.WenForm
    {
        public MsgBox()
        {
            InitializeComponent();
            this.SizeChanged += (s, e) =>
            {
                if (wenImageButton1 != null)
                    wenImageButton1.Width = (this.Width - this.FrameWidth * 2) / 2;
            };
            Text = "消息";
            mesIcon = MsgBoxIcon.Info;
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        public MsgBox(string text) : this()
        {
            Message = text;
        }
        public MsgBox(string text, MsgBoxIcon mesBoxIcon) : this(text)
        {
            MesIcon = mesBoxIcon;
            switch (MesIcon)
            {
                case MsgBoxIcon.Asterisk:
                    Text = "提醒";
                    break;
                case MsgBoxIcon.Error:
                    Text = "错误";
                    break;
                case MsgBoxIcon.Info:
                    Text = "消息";
                    break;
                case MsgBoxIcon.Warning:
                    Text = "警告";
                    break;
                default:
                    break;
            }
        }
        public MsgBox(string text, string caption, MsgBoxIcon msg) : this(text,msg)
        {
            Text = caption;
        }

        #region 私有属性

        private string message;
        private MsgBoxIcon mesIcon;

        #endregion


        #region 公有属性

        public MsgBoxIcon MesIcon
        {
            get => mesIcon;
            set
            {
                mesIcon = value;
                this.Invalidate();
                if (value == MsgBoxIcon.Info)
                {
                    wenImageButton1.Visible = false;
                }
            }
        }

        public string Message
        {
            get => message;
            set
            {
                message = value;

                Graphics g = this.CreateGraphics();
                SizeF sizef = g.MeasureString(value, this.Font);

                int width = sizef.Width > 400 ? 400 : (int)sizef.Width + this.FrameWidth * 2 + 80;
                int height = this.TitleHeight + this.FrameWidth + panel1.Height;

                this.Size = new Size(this.Width < width ? width : this.Width, this.Height < height ? height : this.Height);

                this.Invalidate();
            }
        }

        #endregion

        public enum MsgBoxIcon
        {
            Asterisk,
            Error,
            Info,
            Warning,
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            Rectangle rec = new Rectangle(this.FrameWidth, this.TitleHeight, this.Width - this.FrameWidth * 2, this.Height - this.TitleHeight - this.FrameWidth - panel1.Height);

            Rectangle recStr = new Rectangle(rec.X + 70, rec.Y, rec.Width - 70, rec.Height);

            Rectangle recIco = new Rectangle(rec.X + 5, rec.Y + (rec.Height - 60) / 2, 60, 60);

            Graphics g = e.Graphics;

            using StringFormat sf = new StringFormat(StringFormatFlags.NoClip)
            {
                LineAlignment = StringAlignment.Center,
                Trimming = StringTrimming.EllipsisCharacter
            };

            g.DrawString(Message, Font, new SolidBrush(this.ForeColor), recStr, sf);

            switch (MesIcon)
            {
                case MsgBoxIcon.Asterisk:
                    g.DrawImage(Properties.Resources.Asterisk, recIco);
                    break;
                case MsgBoxIcon.Error:
                    g.DrawImage(Properties.Resources.error, recIco);
                    break;
                case MsgBoxIcon.Info:
                    g.DrawImage(Properties.Resources.Info, recIco);
                    break;
                case MsgBoxIcon.Warning:
                    g.DrawImage(Properties.Resources.Warning, recIco);
                    break;
                default:
                    break;
            }
        }

        private void wenImageButton2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void wenImageButton1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}
